// STKDRV.cpp: implementation of the CSTKDRV class.
//
//////////////////////////////////////////////////////////////////////

#include "STKDRV.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSTKDRV::CSTKDRV()
{
	m_pfnStock_Init = NULL;
	m_pfnStock_Quit = NULL;
	m_pfnGetTotalNumber = NULL;
	m_pfnSetupReceiver = NULL;
	m_pfnGetStockDrvInfo = NULL;
	m_pfnGetStockMin = NULL;
	m_pfnGetStockPRP = NULL;
	m_pfnGetStockPWR = NULL;
	m_pfnGetStockFIN = NULL;

	m_hSTKDrv = NULL;

	GetAdress();
}

CSTKDRV::~CSTKDRV()
{
	if( m_hSTKDrv )  
	{
		FreeLibrary(m_hSTKDrv);
		m_hSTKDrv = NULL;
	}
}

void CSTKDRV::GetAdress()
{
	std::string strAddress("STOCK.DLL");
	m_hSTKDrv = LoadLibrary( strAddress.c_str());
	if( !m_hSTKDrv ) 
		return;
	m_pfnStock_Init = \
		    (int (WINAPI *)(HWND,UINT,int)) GetProcAddress(m_hSTKDrv,"Stock_Init");
	m_pfnStock_Quit = \
			(int (WINAPI *)(HWND)) GetProcAddress(m_hSTKDrv,"Stock_Quit");
	m_pfnGetTotalNumber = \
			(int (WINAPI *)())GetProcAddress(m_hSTKDrv,"GetTotalNumber");
	m_pfnSetupReceiver = \
			(int	(WINAPI *)(BOOL))GetProcAddress(m_hSTKDrv,"SetupReceiver");
	m_pfnGetStockDrvInfo = \
			(DWORD (WINAPI *)(int,void * ))GetProcAddress(m_hSTKDrv,"GetStockDrvInfo");
	m_pfnGetStockMin = \
		    (int (WINAPI *)(char*,int))GetProcAddress(m_hSTKDrv,"GetStockMin");
	m_pfnGetStockPRP = \
			(int (WINAPI *)(char*,int))GetProcAddress(m_hSTKDrv,"GetStockPRP");
	m_pfnGetStockPWR = \
			(int (WINAPI *)(char*,int,int*,char*))GetProcAddress(m_hSTKDrv,"GetStockPwr");
	m_pfnGetStockFIN = \
		    (int (WINAPI *)(char*,int,char*))GetProcAddress(m_hSTKDrv,"GetStockFin");
}

int CSTKDRV::Stock_Init(HWND hWnd, UINT uMsg, int nWorkMode)
{
	//ACE_Guard<ACE_Thread_Mutex> guard(m_ThreadLock);

	if( !m_pfnStock_Init )
		return -1;
	 return( (*m_pfnStock_Init)(hWnd,uMsg,nWorkMode));
}

int CSTKDRV::Stock_Quit(HWND hWnd)
{
	//ACE_Guard<ACE_Thread_Mutex> guard(m_ThreadLock);

	if( !m_pfnStock_Quit )
		return -1;
	return( (*m_pfnStock_Quit)(hWnd));
}

int CSTKDRV::GetTotalNumber()
{
	//ACE_Guard<ACE_Thread_Mutex> guard(m_ThreadLock);

	if( !m_pfnGetTotalNumber )
		return 0;
	return( (*m_pfnGetTotalNumber)());
}

int CSTKDRV::SetupReceiver(BOOL bSetup)
{
	//ACE_Guard<ACE_Thread_Mutex> guard(m_ThreadLock);

	if( !m_pfnSetupReceiver )
		return -1;
	return( (*m_pfnSetupReceiver)(bSetup));
}

DWORD CSTKDRV::GetStockDrvInfo(int nInfo, void *pBuf)
{
	//ACE_Guard<ACE_Thread_Mutex> guard(m_ThreadLock);

	if( !m_pfnGetStockDrvInfo ) 
		return 0;
	return( (*m_pfnGetStockDrvInfo)(nInfo,pBuf));
}

int CSTKDRV::GetStockMin(char * StockCode, int nMarket)
{
	//ACE_Guard<ACE_Thread_Mutex> guard(m_ThreadLock);

	if ( !m_pfnGetStockMin)
		return 0;
	return( (*m_pfnGetStockMin)(StockCode,nMarket));
}

int CSTKDRV::GetStockPRP(char * StockCode, int nMarket)
{
	//ACE_Guard<ACE_Thread_Mutex> guard(m_ThreadLock);

	if ( !m_pfnGetStockPRP)
		return 0;
	return( (*m_pfnGetStockPRP)(StockCode,nMarket));
}

int CSTKDRV::GetStockPWR(char * StockCode, int nMarket, int * PwrNum, char ** pData)
{
	//ACE_Guard<ACE_Thread_Mutex> guard(m_ThreadLock);

	if ( !m_pfnGetStockPWR)
		return 0;
	m_nNum = -1;
	int nRet = ( (*m_pfnGetStockPWR)(StockCode,nMarket,&m_nNum,m_pChar));
	*PwrNum = m_nNum;
	*pData = m_pChar;
	return nRet;
}

int CSTKDRV::GetStockFIN(char * StockCode, int nMarket, char* pPacket)
{
	ACE_Guard<ACE_Recursive_Thread_Mutex> guard(m_ThreadWriteLock);

	if ( !m_pfnGetStockFIN)
		return 0;

	return ( (*m_pfnGetStockFIN)(StockCode,nMarket,pPacket));
	//if (nRet == 1)
	//{
	//	(*pPacket) << uint16(1);
	//	pPacket->WriteStream(m_pChar,sizeof(RCV_FIN_STRUCTEx));
	//}
	//else
	//{
	//	(*pPacket) << uint16(0);
	//}

	//return nRet;
}